package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pick.TenderAllListEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.math.BigDecimal;
import java.util.List;

public class PickHallEsAdapter extends BaseRecycleAdapter<TenderAllListEntity> {

    private PickHomeHotChildHeadAdapter pickHomeHotChildHeadAdapter;
    private IChildClick iChildClick;

    private RecyclerView recyclerView;
    private LinearLayout noList;
    private TextView noListText;
    private ImageView imgTab;
    private LinearLayout linePrice;
    private TextView priceTitle;
    private TextView priceContent;
    private TextView companyTitle;
    private TextView companyContent;
    private TextView addressContent;

    public PickHallEsAdapter(Context context, List<TenderAllListEntity> datas) {
        super(context, R.layout.item_pick_hall_all, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final TenderAllListEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        recyclerView = holder.itemView.findViewById(R.id.child_rv);
        noList = holder.itemView.findViewById(R.id.no_list);
        noListText = holder.itemView.findViewById(R.id.no_list_text);
        imgTab = holder.itemView.findViewById(R.id.img_tab);
        linePrice = holder.itemView.findViewById(R.id.line_price);
        priceTitle = holder.itemView.findViewById(R.id.price_title);
        priceContent = holder.itemView.findViewById(R.id.price_content);
        TextView tvTypeName = holder.itemView.findViewById(R.id.tv_type_name);
        TextView tvEndTime = holder.itemView.findViewById(R.id.tv_endTime);
        TextView name = holder.itemView.findViewById(R.id.name);
        TextView tvVerticalType = holder.itemView.findViewById(R.id.tv_vertical_type);
        TextView textViewWheel = holder.itemView.findViewById(R.id.tv_wheel_num);
        TextView tvType3 = holder.itemView.findViewById(R.id.tv_type3);
        TextView tv_two_type = holder.itemView.findViewById(R.id.tv_two_type);

        companyTitle = holder.itemView.findViewById(R.id.company_title);
        companyContent = holder.itemView.findViewById(R.id.company_content);
        addressContent = holder.itemView.findViewById(R.id.address_content);

        if (entity.getBusinessType() == 3) {
            name.setText(entity.getProjectName());
        } else {
            name.setText(entity.getName());
        }

        if("其他".equals(entity.getTypeName())){
            tvTypeName.setBackgroundResource(R.drawable.bg_red_5);
        }else{
            tvTypeName.setBackgroundResource(R.drawable.pick_list_red);
        }
        switch (entity.getBusinessType()){
            case 1:
                companyTitle.setText("招标企业: ");
                companyContent.setText(entity.getComName());
                addressContent.setText(entity.getRegion() + entity.getAddress());
                break;
            case 2:
                companyTitle.setText("采购单位: ");
                companyContent.setText(entity.getComName());
                addressContent.setText(entity.getRegion() + entity.getAddress());
                break;
            case 3:
                companyTitle.setText("发布单位: ");
                companyContent.setText(entity.getComName());
                addressContent.setText(entity.getAddress());
                break;
        }
        if (entity.getBusinessType() == 1) {   // 1.招标； 2.竞价 3。需求预告
            tv_two_type.setVisibility(View.GONE);
            tvTypeName.setText(entity.getTypeName());
            tvTypeName.setVisibility(View.VISIBLE);
            tvType3.setVisibility(View.GONE);
            // 招标方式：1、资格预审；2、资格后审；3、报价即报名
            if (entity.getVerticalTypeValue() > 0) {
                tvVerticalType.setVisibility(View.VISIBLE);
            } else {
                tvVerticalType.setVisibility(View.GONE);
            }
            if (1 == entity.getVerticalTypeValue()) {
                tvVerticalType.setText("资格预审");
            } else if (2 == entity.getVerticalTypeValue()) {
                tvVerticalType.setText("资格后审");
            } else if (3 == entity.getVerticalTypeValue()) {
                tvVerticalType.setText("报名即报价");
            }
            switch (entity.getState()) {
                case 2:
                case 6:
                    tvEndTime.setText(String.format("投标结束时间：%s", TextUtils.isEmpty(entity.getEndTimeStr(1)) ? "" : entity.getEndTimeStr(1)));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbz));
                    break;
                case 4:
                    tvEndTime.setText(String.format("定标时间：%s", TextUtils.isEmpty(entity.getUpdatedStr(1)) ? "" : entity.getUpdatedStr(1)));
                    imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_zbjs));
                    break;
                case 9:
                    tvEndTime.setText(String.format("报名结束时间：%s", TextUtils.isEmpty(entity.getApplyEndTimeStr(1)) ? "" : entity.getApplyEndTimeStr(1)));
                    imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_bmz));
                    break;
                case 3:
                    tvEndTime.setText(String.format("开标时间：%s", TextUtils.isEmpty(entity.getBidTimeStr(1)) ? "" : entity.getBidTimeStr(1)));
                    imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_pbz));
                    break;
                case 1:
                    tvEndTime.setText(String.format("投标开始时间：%s", TextUtils.isEmpty(entity.getStartTimeStr(1)) ? "" : entity.getStartTimeStr(1)));
                    imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_hall_dzb));
                    break;
                case 8:
                    tvEndTime.setText(String.format("报名开始时间：%s", TextUtils.isEmpty(entity.getApplyStartTimeStr(1)) ? "" : entity.getApplyStartTimeStr(1)));
                    imgTab.setBackground(context.getResources().getDrawable(R.drawable.pick_notice_dbm));
                    break;
            }

            //新增 “招标轮次”     “报名中”9、“待招标”1状态不显示，其他状态显示    接口判断
            if (0 == entity.getRotation() || entity.getState() == 1 || entity.getState() == 8 || entity.getState() == 9) {
                textViewWheel.setVisibility(View.GONE);
            } else {
                textViewWheel.setVisibility(View.VISIBLE);
                textViewWheel.setText("第" + entity.getRotation() + "轮");
            }

            if (entity == null || entity.getGoodsLists() == null) {
                recyclerView.setVisibility(View.GONE);
                noList.setVisibility(View.VISIBLE);
                noListText.setText("该类型无招标清单");
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                noList.setVisibility(View.GONE);
                pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
                pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                    @Override
                    public void onItemClick() {
                        iChildClick.onClick(entity);
                    }
                });
            }
        } else if (entity.getBusinessType() == 2) {
            tv_two_type.setVisibility(View.GONE);
            tvTypeName.setText(entity.getTypeName());
            tvTypeName.setVisibility(View.VISIBLE);

            tvVerticalType.setVisibility(View.GONE);
            textViewWheel.setVisibility(View.GONE);
            tvType3.setVisibility(View.GONE);
            if (2 == entity.getState()) {
                tvEndTime.setText(String.format("截止时间：%s", TextUtils.isEmpty(entity.getDeadlineTime(1)) ? "" : entity.getDeadlineTime(1)));
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_jjz));
            } else if (3 == entity.getState()) {
                tvEndTime.setText(String.format("定标时间：%s", TextUtils.isEmpty(entity.getUpdatedStr(1)) ? "" : entity.getUpdatedStr(1)));
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.icn_yjs));
            } else if (5 == entity.getState()) {
//                imgTab.setBackground(context.getResources().getDrawable(R.drawable.feibiao));
                tvEndTime.setText(String.format("截止时间：%s", TextUtils.isEmpty(entity.getDeadlineTime(1)) ? "" : entity.getDeadlineTime(1)));
                imgTab.setBackground(context.getResources().getDrawable(R.mipmap.pick_tpz));
            } else if (7 == entity.getState()) {
                imgTab.setBackground(context.getResources().getDrawable(R.drawable.liubiao));
            }

            if (entity.getGoodsHeaders() == null || entity.getGoodsLists() == null) {
                recyclerView.setVisibility(View.GONE);
                noList.setVisibility(View.VISIBLE);
                noListText.setText("该类型无竞价清单");
                if (entity.getIsDisplay() == 0) {   //0、隐藏；1、展示；
                    linePrice.setVisibility(View.GONE);
                } else {
                    linePrice.setVisibility(View.VISIBLE);
                    if (entity.getType() == 6 || entity.getType() == 7 || entity.getType() == 8 || entity.getType() == 9 || entity.getType() == 10 || entity.getType() == 13 || entity.getType() == 20) {
                        if (TextUtils.isEmpty(entity.getMinMoney())) {
                            priceTitle.setText("最低报价:  -");
                            priceContent.setText("");
                        } else {
                            priceTitle.setText("最低报价:");
                            BigDecimal bg = new BigDecimal(entity.getMinMoney());
                            String text = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
                            priceContent.setText(text + "元");
                        }
                    }
                }
            } else {
                recyclerView.setVisibility(View.VISIBLE);
                noList.setVisibility(View.GONE);
                linePrice.setVisibility(View.GONE);
                pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
                pickHomeHotChildHeadAdapter.setDisplay(entity.getIsDisplay());
                pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                    @Override
                    public void onItemClick() {
                        iChildClick.onClick(entity);
                    }
                });
            }
        } else if (entity.getBusinessType() == 3) {
            tv_two_type.setVisibility(View.VISIBLE);
            tvTypeName.setVisibility(View.GONE);
            textViewWheel.setVisibility(View.GONE);
            tvType3.setVisibility(View.VISIBLE);
            tvVerticalType.setVisibility(View.GONE);
            //标签
            tv_two_type.setText(entity.getModuleType());//二级分类
            tvType3.setText(entity.getLabel());//一级分类
            String created = entity.getCreated();
            int t = created.indexOf("T");
            tvEndTime.setText(String.format("发布时间：%s", TextUtils.isEmpty(entity.getCreated()) ? "" : created.substring(0, t)));
            imgTab.setVisibility(View.VISIBLE);
            imgTab.setBackground(context.getResources().getDrawable(R.mipmap.pick_zjz));
            pickHomeHotChildHeadAdapter = new PickHomeHotChildHeadAdapter(context, entity.getGoodsHeaders(), entity.getGoodsLists());
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.setAdapter(pickHomeHotChildHeadAdapter);
            pickHomeHotChildHeadAdapter.setDisplay(entity.getIsDisplay());
            pickHomeHotChildHeadAdapter.setOnItemClick(new PickHomeHotChildHeadAdapter.OnItemClick() {
                @Override
                public void onItemClick() {
                    iChildClick.onClick(entity);
                }
            });
        }
    }

    public void onIChildClick(IChildClick iChildClick) {
        this.iChildClick = iChildClick;
    }

    public interface IChildClick {
        void onClick(TenderAllListEntity tenderAllListEntity);
    }
}
